package org.hit.burkun.cases.mapping;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;
import org.hit.burkun.db.DBHelper;
import org.hit.burkun.db.DBTypeConverter;
import org.hit.burkun.dbimport.FileBaseImport;
import org.hit.burkun.file.FileProcessHelper.FILE_TYPE;

public class HgncImport extends FileBaseImport {

	public HgncImport(String fullFileName, FILE_TYPE fileType,
			String tableName, DBHelper dbHelper, int totalPropertyNum,
			boolean hasAutoIncreasementID, DBTypeConverter typeConverter,
			LINE_SPLITER spliterType) {
		super(fullFileName, fileType, tableName, dbHelper, totalPropertyNum,
				hasAutoIncreasementID, typeConverter, spliterType);
	}

	// 去掉双引号
	private Pattern patt = Pattern.compile("\"(.*)\"");

	@Override
	protected String[] forEachItem(String[] items) {
		for (int i = 0; i < 14; i++) {
			Matcher ma = patt.matcher(items[i]);
			if (ma.find()) {
				items[i] = ma.group(1);
			}

			// gene symbol
			if (i == 8 || i == 10) {
				if (items[i].length() > 0) {
					String[] symbols = items[i].split("\\|");
					if (symbols.length > 1) {
						items[i] = StringUtils.join(symbols, ",");
					}
				}
			}
		}
		// 14-17
		for (int i = 14; i < 18; i++) {
			if (items[i].length() > 0) {
				items[i] = items[i].split("T")[0];
			}
		}

		for (int i = 18; i < 48; i++) {
			Matcher ma = patt.matcher(items[i]);
			if (ma.find()) {
				items[i] = ma.group(1);
			}

			// refseq,uniport, pubmed, omim
			if (i == 23 || i == 25 || i == 26 || i == 31) {
				if (items[i].length() > 0) {
					String[] symbols = items[i].split("\\|");
					if (symbols.length > 1) {
						items[i] = StringUtils.join(symbols, ",");
					}
				}
			}
		}

		return items;
	}
}
